import { areaList } from '@vant/area-data';

Page({
    data: {
        address: {},
        totalPrice: 0,
        checkedGoods: [],
        checkLength: 0,
        total: 0,
        areaList,
        name: '',
        checked: [true, false, false],
        isImm:false

    },
    changeAdd() {
        wx.navigateTo({
            url: '../selectAddress/index'
        })
    },
    add() {
        wx.navigateTo({
            url: '../addAddress/index'
        })
    },
    onShow() {
        let imm = wx.getStorageSync('imm')
        let checkedGoods = []
        let that = this
        let token = wx.getStorageSync('token')
        let cart = wx.getStorageSync("cart")
        if(imm===true){
            wx.removeStorageSync('imm')
            that.setData({
                isImm:true
            })
            let good = {
                goods:cart[0],
                num:1
            }
            wx.request({

                url: "https://www.cbing.icu:8095/wxUser/getAddress",
                method: 'GET',
                header: {
                    'Authorization': token
                },
                success(res) {
                    let address = res.data.data
                    let l = address.length - 1
                    // if (index !== "") {
                    //     l = index
                    // }
                    let add = address[l]
                    let final = that.data.areaList.province_list[add.province] + " " + that.data.areaList.city_list[add.city]
                        + " " + that.data.areaList.county_list[add.town] + " " + add.detailaddress
                    that.setData({
                        all: address,
                        address: final,
                        name: address[l].name[0] + '先生',
                        phone: address[l].phone
                    })
                }
            })
            that.setData({
                checkedGoods: [good],
                total: (cart[0].price ).toFixed(2)
            })
        }else {
            that.setData({
                isImm:false
            })
            console.log('ee')
            let total = wx.getStorageSync("total")

            let index = wx.getStorageSync('addIndex')
            wx.request({

                url: "https://www.cbing.icu:8095/wxUser/getAddress",
                method: 'GET',
                header: {
                    'Authorization': token
                },
                success(res) {
                    let address = res.data.data
                    let l = address.length - 1
                    if (index !== "") {
                        l = index
                    }
                    let add = address[l]
                    let final = that.data.areaList.province_list[add.province] + " " + that.data.areaList.city_list[add.city]
                        + " " + that.data.areaList.county_list[add.town] + " " + add.detailaddress
                    that.setData({
                        all: address,
                        address: final,
                        name: address[l].name[0] + '先生',
                        phone: address[l].phone
                    })
                }
            })
            for (let i of cart) {
                if (i.status === 1) {
                    checkedGoods.push(i)
                }
            }
            that.setData({
                checkedGoods: checkedGoods,
                total: (total / 100).toFixed(2)
            })
        }

        // let eventChannel = that.getOpenerEventChannel()
        // if (JSON.stringify(eventChannel) === '{}') {
        // } else {
        //     eventChannel.on('getOrder',  (data) =>{
        //
        //         for(let i of data.cart){
        //             if(i.status===1){
        //                 checkedGoods.push(i)
        //             }
        //         }
        //
        //     })
        // }

        // console.log(that.data.ca)
    },
    onSubmit() {
        let token = wx.getStorageSync("token")
        let that = this
        if (that.data.name === "") {
            wx.showToast({
                title: "请先添加地址"
                ,
                duration: 2000,
                icon: 'none'

            })
        } else if(that.data.isImm){
            console.log(that.data.checkedGoods[0].goods.id)
            wx.request({
                url: "https://www.cbing.icu:8095/wxUser/createOrder?imm="+true+'&goodId='+that.data.checkedGoods[0].goods.id,
                method: "GET",
                header: {
                    'Authorization': token
                },
                success(res) {
                    console.log(res)
                    let date = new Date()
                    let o = {
                        "M": date.getMonth() + 1, //月份
                        "d": date.getDate(), //日
                        "h": date.getHours(), //小时
                        "m": date.getMinutes(), //分
                        "s": date.getSeconds(), //秒
                        "S": date.getMilliseconds(),
                        "Y": date.getFullYear()
                    }
                    let date1 = o.Y + "年" + o.M + "月" + o.d + "日" + "  " + o.h + ":" + o.m + ":" + o.s
                    // 通过 eventChannel 向被打开页面传送数据
                    wx.setStorageSync("checked", that.data.checked)
                    wx.removeStorageSync('cart')
                    // wx.setStorage({
                    //     key: "dateDone",
                    //     data: date1,
                    //     success(){
                    //         wx.redirectTo({
                    //             url: "../paydone/index",
                    //         })
                    //     }
                    // })
                    wx.setStorageSync("dateDone", date1)
                    let addr = {
                        address: that.data.address,
                        name: that.data.name,
                        phone:that.data.phone
                    }
                    wx.setStorageSync('checkGoods',that.data.checkedGoods)
                    wx.setStorageSync('address',addr)
                    wx.redirectTo({
                        url: "../paydone/index",
                    })
                }
            })
        }else {
            wx.request({
                url: "https://www.cbing.icu:8095/wxUser/createOrder",
                method: "GET",
                header: {
                    'Authorization': token
                },
                success(res) {
                    console.log(res)
                    let date = new Date()
                    let o = {
                        "M": date.getMonth() + 1, //月份
                        "d": date.getDate(), //日
                        "h": date.getHours(), //小时
                        "m": date.getMinutes(), //分
                        "s": date.getSeconds(), //秒
                        "S": date.getMilliseconds(),
                        "Y": date.getFullYear()
                    }
                    let date1 = o.Y + "年" + o.M + "月" + o.d + "日" + "  " + o.h + ":" + o.m + ":" + o.s
                    // 通过 eventChannel 向被打开页面传送数据
                    wx.setStorageSync("checked", that.data.checked)
                    wx.removeStorageSync('cart')
                    wx.setStorageSync('checkGoods',that.data.checkedGoods)
                    wx.setStorageSync("dateDone", date1)
                    let addr = {
                        address: that.data.address,
                        name: that.data.name,
                        phone:that.data.phone
                    }
                    wx.setStorageSync('address',addr)
                    wx.redirectTo({
                        url: "../paydone/index",
                    })
                }
            })
        }


    },
    onChange(e) {
        let index = e.currentTarget.dataset.index
        let c = [false, false, false]
        c[index] = true
        this.setData({
            checked: c
        })
    }
})